﻿using System.Collections.Generic;
using System.Text;

namespace Safebrowsing
{
    /// <summary>
    /// Part of the data request.
    /// Contains list name and the numbers of chunks stored in this list.
    /// </summary>
    internal class DataListRequest
    {
        /// <summary>
        /// Creates data list request
        /// </summary>
        /// <param name="safebrowsingList"></param>
        /// <param name="addChunksNumbers"></param>
        /// <param name="subChunksNumbers"></param>
        internal DataListRequest(SafebrowsingList safebrowsingList, IEnumerable<int> addChunksNumbers, IEnumerable<int> subChunksNumbers)
        {
            List = safebrowsingList;
            AddChunksNumbers = new List<int>(addChunksNumbers ?? new int[]{});
            SubChunksNumbers = new List<int>(subChunksNumbers ?? new int[]{});
        }

        /// <summary>
        /// Requested list
        /// </summary>
        public SafebrowsingList List { get; set; }

        /// <summary>
        /// List of add-chunks numbers
        /// </summary>
        public List<int> AddChunksNumbers { get; private set; }

        /// <summary>
        /// List of sub-chunks numbers
        /// </summary>
        public List<int> SubChunksNumbers { get; private set; }

        public override string ToString()
        {
            var sb = new StringBuilder();
            sb.Append(List.Name);
            sb.Append(';');
            
            if (AddChunksNumbers.Count > 0)
            {
                sb.Append("a:");
                sb.Append(Utils.JoinRangeList(AddChunksNumbers));
            }

            if (SubChunksNumbers.Count > 0)
            {
                if (AddChunksNumbers.Count > 0) sb.Append(":");
                sb.Append("s:");
                sb.Append(Utils.JoinRangeList(SubChunksNumbers));
            }

            return sb.ToString();
        }
    }
}
